package get

import (
	"database/sql"
	"fmt"

	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
)

func Register_account(r *gin.Engine) {
	var err error
	var db *sql.DB
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)
	db, err = sql.Open("postgres", psqlInfo)
	var username, userpassword string

	r.GET("/register", func(c *gin.Context) {
		// 获取参数
		username = c.Query("username")
		userpassword = c.Query(("userpassword"))
		c.JSON(200, gin.H{
			"username":     username,
			"userpassword": userpassword,
		})

	})

	fmt.Printf("%s%s", username, userpassword)

	r.GET("/registersuccess", func(c *gin.Context) {
		var code string
		code = "0"

		if err != nil {
			panic(err)
		}

		stmt, err := db.Prepare("INSERT INTO public.web(username,userpassword) VALUES($1,$2) RETURNING userid")
		if err != nil {
			panic(err)
		}

		res, err := stmt.Exec(username, userpassword)
		if err != nil {
			panic(err)
		}

		affect, err := res.RowsAffected()
		if err != nil {
			panic(err)
		}

		fmt.Println("rows affect:", affect)

		code = "10"

		c.JSON(200, gin.H{
			"code":     code,
			"password": userpassword,
		})
	})
}
